package com.khtec.tools.parsetool;

import com.khtec.tools.parsetool.common.FormatUtil;
import com.khtec.tools.parsetool.common.MessageUtil;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ParseToolDesktopApp {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGUI());
    }

    public static void createAndShowGUI() {
        // 创建主窗口
        JFrame frame = new JFrame("Format - 字符串格式化和报文处理工具");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        // 创建选项卡面板
        JTabbedPane tabbedPane = new JTabbedPane();

        // 添加格式化工具面板
        tabbedPane.addTab("字符串格式化", createFormattingPanel());

        // 添加报文处理面板
        tabbedPane.addTab("报文处理", createMessagePanel());

        frame.add(tabbedPane, BorderLayout.CENTER);

        // 设置窗口属性
        frame.setPreferredSize(new Dimension(800, 600));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static JPanel createFormattingPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 输入区域
        JPanel inputPanel = new JPanel(new BorderLayout());
        inputPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "输入", TitledBorder.LEFT, TitledBorder.TOP));

        JTextArea inputArea = new JTextArea(10, 50);
        inputArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        JScrollPane inputScrollPane = new JScrollPane(inputArea);
        inputPanel.add(inputScrollPane, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout());

        JButton jsonFormatButton = new JButton("美化JSON");
        JButton xmlFormatButton = new JButton("美化XML");

        buttonPanel.add(jsonFormatButton);
        buttonPanel.add(xmlFormatButton);

        // 输出区域
        JPanel outputPanel = new JPanel(new BorderLayout());
        outputPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "输出", TitledBorder.LEFT, TitledBorder.TOP));

        JTextArea outputArea = new JTextArea(10, 50);
        outputArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        outputArea.setEditable(false);
        JScrollPane outputScrollPane = new JScrollPane(outputArea);
        outputPanel.add(outputScrollPane, BorderLayout.CENTER);

        // 添加组件到主面板
        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(buttonPanel, BorderLayout.CENTER);
        panel.add(outputPanel, BorderLayout.SOUTH);

        // 添加按钮事件监听器
        jsonFormatButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String input = inputArea.getText();
                    if (input.trim().isEmpty()) {
                        outputArea.setText("请输入要格式化的内容");
                        return;
                    }
                    String formatted = FormatUtil.prettyJson(input);
                    outputArea.setText(formatted);
                } catch (Exception ex) {
                    outputArea.setText("JSON格式化错误: " + ex.getMessage());
                }
            }
        });

        xmlFormatButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String input = inputArea.getText();
                    if (input.trim().isEmpty()) {
                        outputArea.setText("请输入要格式化的内容");
                        return;
                    }
                    String formatted = FormatUtil.prettyXml(input);
                    outputArea.setText(formatted);
                } catch (Exception ex) {
                    outputArea.setText("XML格式化错误: " + ex.getMessage());
                }
            }
        });

        return panel;
    }

    private static JPanel createMessagePanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 输入区域
        JPanel inputPanel = new JPanel(new BorderLayout());
        inputPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "报文内容", TitledBorder.LEFT, TitledBorder.TOP));

        JTextArea messageArea = new JTextArea(8, 50);
        messageArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        JScrollPane messageScrollPane = new JScrollPane(messageArea);
        inputPanel.add(messageScrollPane, BorderLayout.CENTER);

        // 配置面板
        JPanel configPanel = new JPanel(new FlowLayout());
        configPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "配置", TitledBorder.LEFT, TitledBorder.TOP));

        JCheckBox needHeaderCheckBox = new JCheckBox("添加报文头", true);
        JLabel headerLengthLabel = new JLabel("报文头长度:");
        JTextField headerLengthField = new JTextField("8", 5);

        configPanel.add(needHeaderCheckBox);
        configPanel.add(headerLengthLabel);
        configPanel.add(headerLengthField);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout());

        JButton buildMessageButton = new JButton("生成报文");
//        JButton parseMessageButton = new JButton("解析报文");
//        JButton submitButton = new JButton("提交");

        configPanel.add(buildMessageButton);
//        configPanel.add(parseMessageButton);
//        configPanel.add(submitButton);

        // 输出区域
        JPanel outputPanel = new JPanel(new BorderLayout());
        outputPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "结果", TitledBorder.LEFT, TitledBorder.TOP));

        JTextArea resultArea = new JTextArea(8, 50);
        resultArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        resultArea.setEditable(false);
        JScrollPane resultScrollPane = new JScrollPane(resultArea);
        outputPanel.add(resultScrollPane, BorderLayout.CENTER);

        // 添加组件到主面板
        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(configPanel, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);
        panel.add(outputPanel, BorderLayout.PAGE_END);

        // 添加按钮事件监听器
        buildMessageButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String message = messageArea.getText();
                    if (message.trim().isEmpty()) {
                        resultArea.setText("请输入报文内容");
                        return;
                    }

                    boolean needHeader = needHeaderCheckBox.isSelected();
                    int headerLength = Integer.parseInt(headerLengthField.getText());

                    String result = MessageUtil.buildMessage(message, needHeader, headerLength);
                    resultArea.setText(result);
                } catch (NumberFormatException ex) {
                    resultArea.setText("报文头长度必须是数字");
                } catch (Exception ex) {
                    resultArea.setText("生成报文错误: " + ex.getMessage());
                }
            }
        });

//        parseMessageButton.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                try {
//                    String fullMessage = messageArea.getText();
//                    if (fullMessage.trim().isEmpty()) {
//                        resultArea.setText("请输入完整报文");
//                        return;
//                    }
//
//                    int headerLength = Integer.parseInt(headerLengthField.getText());
//
//                    String result = MessageUtil.parseMessage(fullMessage, headerLength);
//                    resultArea.setText(result);
//                } catch (NumberFormatException ex) {
//                    resultArea.setText("报文头长度必须是数字");
//                } catch (Exception ex) {
//                    resultArea.setText("解析报文错误: " + ex.getMessage());
//                }
//            }
//        });

        // 提交按钮事件监听器
//        submitButton.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                try {
//                    String message = messageArea.getText();
//                    if (message.trim().isEmpty()) {
//                        resultArea.setText("请输入报文内容");
//                        return;
//                    }
//
//                    boolean needHeader = needHeaderCheckBox.isSelected();
//                    int headerLength = Integer.parseInt(headerLengthField.getText());
//
//                    // 如果需要报文头，则生成带报文头的报文；否则直接显示原报文
//                    String result;
//                    if (needHeader) {
//                        result = MessageUtil.buildMessage(message, needHeader, headerLength);
//                        resultArea.setText("已提交带报文头的报文:\n" + result);
//                    } else {
//                        result = message;
//                        resultArea.setText("已提交报文:\n" + result);
//                    }
//
//                    // 这里可以添加实际的提交逻辑，比如发送到服务器等
//                    JOptionPane.showMessageDialog(panel, "报文提交成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
//                } catch (NumberFormatException ex) {
//                    resultArea.setText("报文头长度必须是数字");
//                } catch (Exception ex) {
//                    resultArea.setText("提交报文错误: " + ex.getMessage());
//                }
//            }
//        });

        return panel;
    }
}
